﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.Data.Entity;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_HoSo_GiayToController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        //
        // GET: /GPS_HTCapGPLX/
        #region Index
        public ActionResult Index(ViewModelSHOSO_GIAYTO SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<SHOSO_GIAYTO>(o => (SearchModel.hHSGIAYTO_MA == null || o.HSGIAYTO_MA.StartsWith(SearchModel.hHSGIAYTO_MA))
                                        && (SearchModel.hHSGIAYTO_HOSO_MA == null || o.HSGIAYTO_HOSO_MA.StartsWith(SearchModel.hHSGIAYTO_HOSO_MA))
                                        && (SearchModel.hHSGIAYTO_ACTIVE == null || o.HSGIAYTO_ACTIVE == SearchModel.hHSGIAYTO_ACTIVE)
                                        && (SearchModel.hHSGIAYTO_HGPLX_MA == null || o.HSGIAYTO_HGPLX_MA == SearchModel.hHSGIAYTO_HGPLX_MA)
                                        );
            ViewBag.TotalRow = entities.Count();
            var model = new List<SHOSO_GIAYTO>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<SHOSO_GIAYTO>(currentEntity));
            }
            Int16 icount = 1;
            foreach (var item in model)
            {
                item.STT = icount;
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]
        public void initialCategorySearchAction(ViewModelSHOSO_GIAYTO viewModel)
        {
            ViewBag.lstSGT = new SelectList(_db.GetAll<SGT>(), "GT_MA", "GT_TENVN");
            ViewBag.lstSHangDT = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");
            ViewBag.lstHoSo = new SelectList(_db.GetAll<SHOSO>(), "HOSO_MA", "HOSO_TEN");
           // viewModel.lstHOSO = _db.GetAll<SHOSO>();
        }
        #endregion

        //
        // Create: 
        #region create
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelSHOSO_GIAYTO());
        }

        [HttpPost]
        public ActionResult Create(CrudModelSHOSO_GIAYTO model)
        {
            initialCategoryCreateAction();
            if (ModelState.IsValid)
            {
                //check trùng
                string strcheck = _db.Filter<SHOSO_GIAYTO>(x => x.HSGIAYTO_MA == model.HSGIAYTO_MA
                                                    && x.HSGIAYTO_HOSO_MA == model.HSGIAYTO_HOSO_MA
                                                    && x.HSGIAYTO_HGPLX_MA == model.HSGIAYTO_HGPLX_MA).Select(x=>x.HSGIAYTO_MA).FirstOrDefault().ToString();
                if (string.IsNullOrEmpty(strcheck))
                {
                    SHOSO_GIAYTO _model = new SHOSO_GIAYTO();
                    GPS.COMMON.Helpers.CopyObject<SHOSO_GIAYTO>(model, ref _model);
                    _db.Insert<SHOSO_GIAYTO>(_model);
                    return RedirectToAction("Index");
                }
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
            ViewBag.lstSGT = new SelectList(_db.GetAll<SGT>(), "GT_MA", "GT_TENVN");
            ViewBag.lstSHangDT = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");
            ViewBag.lstHoSo = new SelectList(_db.GetAll<SHOSO>(), "HOSO_MA", "HOSO_TEN");           
        }

        #endregion

        //
        // Edit: 
        #region Edit
        public ActionResult Edit(string idGT, string idHGPLX, string idHS)
        {
            CrudModelSHOSO_GIAYTO _model = new CrudModelSHOSO_GIAYTO();
            SHOSO_GIAYTO model = _db.GetOne<SHOSO_GIAYTO>(o => o.HSGIAYTO_MA == idGT
                                                            && o.HSGIAYTO_HGPLX_MA == idHGPLX
                                                            && o.HSGIAYTO_HOSO_MA == idHS);
            GPS.COMMON.Helpers.CopyObject<CrudModelSHOSO_GIAYTO>(model, ref _model);
            initialCategoryEditAction(_model);
            return View(_model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelSHOSO_GIAYTO model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<SHOSO_GIAYTO>(o => o.HSGIAYTO_MA == model.HSGIAYTO_MA);
                GPS.COMMON.Helpers.CopyObject<SHOSO_GIAYTO>(model, ref _model);
                _db.Update<SHOSO_GIAYTO>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryEditAction(CrudModelSHOSO_GIAYTO view)
        {
            string selectedSGT = _db.GetAll<SGT>().Select(x => x.GT_MA = view.HSGIAYTO_MA).FirstOrDefault().ToString();
            ViewBag.lstSGT = new SelectList(_db.GetAll<SGT>(), "GT_MA", "GT_TENVN", selectedSGT);

            string selectedSH = _db.GetAll<SHANGGPLX>().Select(x => x.HGPLX_MA = view.HSGIAYTO_HGPLX_MA).FirstOrDefault().ToString();
            ViewBag.lstSHangDT = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN", selectedSH);

            string selectedSGT1 = _db.GetAll<SHOSO>().Select(x => x.HOSO_MA = view.HSGIAYTO_HOSO_MA).FirstOrDefault().ToString();
            ViewBag.lstHoSo = new SelectList(_db.GetAll<SHOSO>(), "HOSO_MA", "HOSO_TEN", selectedSGT1);        
        }
        #endregion

        #region Detele
        public ActionResult Delete(string idGT, string idHGPLX, string idHS)
        {
            CrudModelSHOSO_GIAYTO _model = new CrudModelSHOSO_GIAYTO();
            SHOSO_GIAYTO model = _db.GetOne<SHOSO_GIAYTO>(o => o.HSGIAYTO_MA == idGT
                                                            && o.HSGIAYTO_HGPLX_MA == idHGPLX
                                                            && o.HSGIAYTO_HOSO_MA == idHS);
            _db.DeleteItem<SHOSO_GIAYTO>(model);
            return RedirectToAction("Index");
        }
        #endregion

        // Detail
        #region Detail
        public ActionResult Detail(string idGT, string idHGPLX, string idHS)
        {
            CrudModelSHOSO_GIAYTO _model = new CrudModelSHOSO_GIAYTO();
            SHOSO_GIAYTO model = _db.GetOne<SHOSO_GIAYTO>(o => o.HSGIAYTO_MA == idGT
                                                           && o.HSGIAYTO_HGPLX_MA == idHGPLX
                                                           && o.HSGIAYTO_HOSO_MA == idHS);
            GPS.COMMON.Helpers.CopyObject<CrudModelSHOSO_GIAYTO>(model, ref _model);
            initialCategoryDetailAction(_model);
            return PartialView("Detail", _model);
        }

        public void initialCategoryDetailAction(CrudModelSHOSO_GIAYTO view)
        {
            ViewBag.txtTrangThai = "Không Hiệu Lực";
            if (view.HSGIAYTO_ACTIVE == "1")
            {
                view.HSGIAYTO_ACTIVE = "Hiệu Lực";
            }
            SGT gt = _db.GetOne<SGT>(o => o.GT_MA == view.HSGIAYTO_MA);
            view.HSGIAYTO_MA = gt.GT_TENVN;
            SHANGGPLX hang = _db.GetOne<SHANGGPLX>(o => o.HGPLX_MA == view.HSGIAYTO_HGPLX_MA);
            view.HSGIAYTO_HGPLX_MA = hang.HGPLX_TEN;
            SHOSO hs = _db.GetOne<SHOSO>(o => o.HOSO_MA == view.HSGIAYTO_HOSO_MA);
            view.HSGIAYTO_HOSO_MA = hs.HOSO_TEN;
        }
        #endregion
    }
}